package com.toogu.mazhangmen.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

public class RadioGroupView extends LinearLayout implements View.OnClickListener {

    private Context context;
    private LinearLayout layout;
    private List<TextView> textViewList;
    private String[] itemName;

    public interface OnCheckedChangeListener{
        void onCheckedChanged(int situation);
    }

    private OnCheckedChangeListener onCheckedChangeListener;

    public RadioGroupView(Context context) {
        super(context);
        init(context);
    }

    public RadioGroupView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RadioGroupView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.context=context;
        setGravity(Gravity.CENTER|Gravity.LEFT);
        View view = LayoutInflater.from(context).inflate(R.layout.view_radio_group, null, false);
        layout=  view.findViewById(R.id.layout);
        layout.setGravity(Gravity.CENTER|Gravity.LEFT);
        textViewList=new ArrayList<>();
        addView(view);
    }


    public void setInit(String[] itemName){
        textViewList.clear();
        layout.removeAllViews();
        this.itemName=itemName;
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(UiUtil.dip2px(5),UiUtil.dip2px(5),UiUtil.dip2px(5),UiUtil.dip2px(5));
        for(String name:itemName){
            TextView textView=createText(name);
            textViewList.add(textView);
            layout.addView(textView,layoutParams);
        }
    }

    private TextView createText(String content) {
//        while (content.length()<12){
//            content=" "+content+" ";
//        }
        TextView view = (TextView) LayoutInflater.from(context).inflate(R.layout.view_radio_button, null, false);
        view.setText(content);
        view.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        int situation = textViewList.indexOf((TextView) v);
        select(situation);
        onCheckedChangeListener.onCheckedChanged(situation);
    }

    public void select(int situation){
        for(int i=0;i<textViewList.size();i++){
            if(i==situation) textViewList.get(i).setTextColor(Color.RED);
            else textViewList.get(i).setTextColor(Color.WHITE);
        }

    }

    public void setWithoutChoice(){
        for(TextView textView:textViewList)
            textView.setTextColor(Color.WHITE);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener){
        this.onCheckedChangeListener=onCheckedChangeListener;
    }


}
